#!/usr/bin/env bash

# AzerothCore Test Common Utilities
# Shared functions and setup for all BATS tests

export AC_TEST_FRAMEWORK_VERSION="1.0.0"

# Get paths
AC_TEST_FRAMEWORK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
AC_PROJECT_ROOT="$(cd "$AC_TEST_FRAMEWORK_DIR/../.." && pwd)"

# Common test environment setup
setup_test_env() {
    export TEST_DIR="$(mktemp -d)"
    export AC_TEST_ROOT="$AC_PROJECT_ROOT"
    export AC_TEST_APPS="$AC_TEST_ROOT/apps"
    
    # Create standard test directory structure
    mkdir -p "$TEST_DIR"/{bin,etc,logs,data,crashes,build}
    
    # Set up test-specific environment variables
    export ORIGINAL_PATH="$PATH"
    export PATH="$TEST_DIR/bin:$PATH"
    
    # Common environment variables for AzerothCore
    export BUILDPATH="$TEST_DIR/build"
    export SRCPATH="$AC_TEST_ROOT"
    export BINPATH="$TEST_DIR/bin"
    export LOGS_PATH="$TEST_DIR/logs"
}

cleanup_test_env() {
    if [[ -n "$TEST_DIR" && -d "$TEST_DIR" ]]; then
        rm -rf "$TEST_DIR"
    fi
    if [[ -n "$ORIGINAL_PATH" ]]; then
        export PATH="$ORIGINAL_PATH"
    fi
}

# Create standard test binary
create_test_binary() {
    local binary_name="$1"
    local exit_code="${2:-0}"
    local runtime="${3:-2}"
    local extra_output="${4:-""}"
    
    cat > "$TEST_DIR/bin/$binary_name" << EOF
#!/usr/bin/env bash
echo "$binary_name starting with config: \$2"
echo "$binary_name running for $runtime seconds..."
if [[ -n "$extra_output" ]]; then
    echo "$extra_output"
fi
sleep $runtime
echo "$binary_name exiting with code $exit_code"
exit $exit_code
EOF
    chmod +x "$TEST_DIR/bin/$binary_name"
}

# Create test configuration file
create_test_config() {
    local config_name="$1"
    local content="$2"
    
    cat > "$TEST_DIR/etc/$config_name" << EOF
# Test configuration file: $config_name
# Generated by AzerothCore test framework
$content
EOF
}

# Create AzerothCore specific test binaries
create_acore_binaries() {
    create_test_binary "authserver" 0 1 "AuthServer initialized"
    create_test_binary "worldserver" 0 2 "WorldServer initialized"
    create_test_binary "cmake" 0 1 "CMake configured"
    create_test_binary "make" 0 2 "Build completed"
    create_test_binary "mapextractor" 0 3 "Map extraction completed"
    create_test_binary "vmap4extractor" 0 2 "VMap extraction completed"
    create_test_binary "vmap4assembler" 0 1 "VMap assembly completed"
    create_test_binary "mmaps_generator" 0 5 "MMap generation completed"
}

# Create AzerothCore specific test configs
create_acore_configs() {
    create_test_config "authserver.conf" 'Database.Info = "127.0.0.1;3306;acore;acore;acore_auth"
LoginDatabaseInfo = "127.0.0.1;3306;acore;acore;acore_auth"'
    
    create_test_config "worldserver.conf" 'Database.Info = "127.0.0.1;3306;acore;acore;acore_world"
LoginDatabaseInfo = "127.0.0.1;3306;acore;acore;acore_auth"
CharacterDatabaseInfo = "127.0.0.1;3306;acore;acore;acore_characters"'
    
    create_test_config "config.sh" "export BUILDPATH=\"$TEST_DIR/build\"
export SRCPATH=\"$AC_TEST_ROOT\"
export BINPATH=\"$TEST_DIR/bin\"
export LOGS_PATH=\"$TEST_DIR/logs\""
}

# Create a test script configuration (for startup scripts)
create_test_script_config() {
    local script_name="$1"
    local binary_name="${2:-authserver}"
    
    cat > "$TEST_DIR/conf-$script_name.sh" << EOF
export BINPATH="$TEST_DIR/bin"
export SERVERBIN="$binary_name"
export CONFIG="$TEST_DIR/etc/$binary_name.conf"
export LOGS_PATH="$TEST_DIR/logs"
export LOG_PREFIX_NAME="$script_name"
export SCREEN_NAME="AC-$script_name"
export GDB_ENABLED=0
export WITH_CONSOLE=1
EOF
}

# Debug helper function
debug_on_failure() {
    if [[ "$status" -ne 0 ]]; then
        echo "Command failed with status: $status" >&3
        echo "Output was:" >&3
        echo "$output" >&3
        if [[ -n "$TEST_DIR" ]]; then
            echo "Test directory contents:" >&3
            ls -la "$TEST_DIR" >&3 2>/dev/null || true
        fi
    fi
}

# Print test environment info
print_test_env() {
    echo "Test Environment:" >&3
    echo "  TEST_DIR: $TEST_DIR" >&3
    echo "  AC_TEST_ROOT: $AC_TEST_ROOT" >&3
    echo "  AC_TEST_APPS: $AC_TEST_APPS" >&3
    echo "  PATH: $PATH" >&3
}

# Check if running in test mode
is_test_mode() {
    [[ -n "$BATS_TEST_FILENAME" ]] || [[ -n "$TEST_DIR" ]]
}
